package cn.ddiancan.schedule.task.listener;

import cn.ddiancan.schedule.task.CronTaskRegistrar;
import cn.ddiancan.schedule.task.event.TaskInvalidEvent;
import cn.ddiancan.schedule.task.service.SchedulerService;
import cn.ddiancan.schedule.task.service.SchedulingRunnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class TaskMonitroListener implements ApplicationListener<TaskInvalidEvent> {
    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;


    @Override
    public void onApplicationEvent(TaskInvalidEvent event) {
        String key = event.getTaskCode();
        Object source = event.getSource();
        if (source instanceof SchedulingRunnable task) {
            cronTaskRegistrar.removeCronTask(task);
            SchedulerService.extutorMap.remove(key);
        }
    }
}
